<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model;

use app\common\model\RecommendGoods as RecommendGoodsModel;

/**
 * 商品推荐内容与推荐商品模型
 * Class GoodsCategoryRel
 * @package app\store\model
 */
class RecommendGoods extends RecommendGoodsModel
{
 

    /**
     * 获取推荐商品ID集
     * @param int $goodsId
     * @return array
     */
    public static function getRecommendGoodsIds(int $recommendId)
    {
        return (new static)->where('recommend_id', '=', $recommendId)->column('goods_id');
    }

    /**
     * 批量写入商品分类记录
     * @param int $goodsId
     * @param array $categoryIds
     * @return array|false
     */
    public static function increased(int $recommendId, array $goods_ids)
    {
        $dataset = [];
        foreach ($goods_ids as $goods_id) {
            $dataset[] = [
                'goods_id' => $goods_id,
                'recommend_id' => $recommendId,
                'store_id' => self::$storeId
            ];
        }
        return (new static)->addAll($dataset);
    }

    /**
     * 更新关系记录
     * @param $goodsId
     * @param array $categoryIds 新的分类集
     * @return array|false
     * @throws \Exception
     */
    public static function updates(int $recommendId, $goods_ids)
    {
        // 删除所有的商品记录
        static::deleteAll(['recommend_id' => $recommendId]);
        // 批量写入商品图片记录
        return static::increased($recommendId, $goods_ids);
    }



}
